from odoo import fields, models, api, _


class DiffRequisitionLineWizard(models.TransientModel):
    _name = 'diff.requisition.line.wizard'
    _description = 'Purchase Requisition Create Wizard'

    prm_id = fields.Many2one('diff.requisition.main.wizard', string='Line', required=True)
    pr_line_id = fields.Many2one('sb.presets.bom.diff.line', string='Purchase Requisition Line')
    product_id = fields.Many2one('product.product', related='pr_line_id.product_id', string='Product')
    uom_id = fields.Many2one('uom.uom', related='pr_line_id.product_uom_id', string='产品单位')
    product_uom_id = fields.Many2one('uom.uom', '请购单位', related='product_id.uom_po_id', store=True)
    panel_qty = fields.Float('Quantity', digits=(10, 6))  # 请购数量


class DiffRequisitionMainWizard(models.TransientModel):
    _name = 'diff.requisition.main.wizard'
    _description = 'Purchase Requisition Main Wizard'

    diff_requisition_id = fields.Many2one('sb.presets.bom.diff', string='Purchase Requisition')
    product_categ_id = fields.Many2one('product.category', string="Product Category",
                                       domain="[('parent_id', '=', False),('code', '!=', False)]")
    remark = fields.Text('Remark', copy=False)
    prc_line_ids = fields.One2many('diff.requisition.line.wizard', 'prm_id', 'prc_line')

    def confirmation(self):
        self = self.sudo()
        line = []
        for rec in self.prc_line_ids:
            line.append((0, 0, {
                'product_id': rec.product_id.product_tmpl_id.id,
                'name': rec.product_id.display_name,
                'product_qty': rec.panel_qty,
            }))
        data = {
            'product_categ_id': self.product_categ_id.id,
            'remark': self.remark,
            'purchase_requisition_type': 'material',
            'pr_line_ids': line
        }
        requisition = self.env['purchase.requisition'].create(data)
        view_id = self.env.ref('sb_workflow_purchase.purchase_requisition_view_form').id
        self.diff_requisition_id.state = 'generate'
        return {
            'name': _('Purchase Requisition'),
            'type': 'ir.actions.act_window',
            'res_model': 'purchase.requisition',
            'res_id': requisition.id,
            'view_id': view_id,
            'view_mode': 'form',
            'target': 'current',
        }


class PresetsBomWizard(models.Model):
    _name = 'sb.presets.bom.wizard'
    _description = '预设BOM Wizard'

    diff_requisition_id = fields.Many2one('sb.presets.bom.diff', string='Purchase Requisition')
    purchase_ids = fields.Many2many('purchase.order', string='Purchase Order', compute='_compute_purchase_ids')
    requisition_ids = fields.Many2many('purchase.requisition', string='Purchase Requisition')

    @api.depends('requisition_ids')
    def _compute_purchase_ids(self):
        """计算采购单"""
        for rec in self:
            rec.purchase_ids = rec.requisition_ids.mapped('purchase_ids')

    def confirmation(self):
        """执行确认"""
        self.diff_requisition_id.state = 'confirmed'
